#!/usr/bin/env python3
# coding:utf-8
import sys
import copy
import numpy as np
# import cv2
import cv2 as cv

# Create a black image
class draw_obs:
    def __init__(self, width, height, scaling=4):
        # 单元格数量
        self.width: int = width
        self.height: int = height
        self.mid_w: int = int(self.width / 2)

        # x,y 坐标数值
        self.unitSize: int = 10
        self.num_x: int = self.width * self.unitSize
        self.num_y: int = self.height * self.unitSize
        
        # 转换中心对称x,y值
        self.offsetX: int = self.mid_w * self.unitSize
        # self.offsety = -self.height * self.unitSize

        # 画布大小 w宽 h高
        self.scaling: int = scaling                      # 画布放大系数
        self.imgCell: int = self.scaling * self.unitSize # 单元格大小
        self.img_w: int = self.width * self.imgCell
        self.img_h: int = self.height * self.imgCell

        self.img = np.zeros(
            (
                self.img_h + 2* self.imgCell,
                self.img_w + 2* self.imgCell,
                3,
            ),
            np.uint8,
        )
        self.draw_table()
        self.draw_table_text()
        self.table_org_img = copy.copy(self.img)

    def draw_rectangle(self,x,y,size):
        s1_x = x + self.offsetX
        s1_y = self.num_y - y

        # 方法1: 从右到左
        s2_x = s1_x+size
        
        # 方法2: 中心Y轴, 两边辐射
        # if x > 0:
        #     s2_x = s1_x+size
        # elif x < 0:
        #     s2_x = s1_x-size
        # elif x==0:
        #     org_x = s1_x
        #     s1_x = org_x-int(size/2)
        #     s2_x = org_x+int(size/2)
        s2_y = s1_y - size

        cv.rectangle(self.img,(s1_x*self.scaling,s1_y*self.scaling),(s2_x*self.scaling,s2_y*self.scaling),(0,255,0),-1)

    def draw_table(self):
        for x in range(self.width):
            point_a = (x * self.imgCell, 0)
            point_b = (x * self.imgCell, self.height * self.imgCell)
            cv.line(self.img, point_a, point_b, (255, 0, 0), 2)
        for y in range(self.height):
            point_a = (0, y * self.imgCell)
            point_b = (self.width * self.imgCell, y * self.imgCell)
            cv.line(self.img, point_a, point_b, (255, 0, 0), 1)
            
        # 中心对称轴 红线
        self.draw_show()
        point_a = (self.mid_w*self.imgCell,0)
        point_b = (self.mid_w*self.imgCell,self.height * self.imgCell)
        cv.line(self.img, point_a, point_b, (0, 0, 255), 2)
        self.draw_show()

    def draw_table_text(self):
        font = cv.FONT_HERSHEY_SIMPLEX
        text_x = -(self.mid_w* self.unitSize)
        for x in range(self.width):
            cv.putText(self.img,str(text_x),
                (x * self.imgCell, self.height * self.imgCell),
                font,0.4,(255, 255, 255),2,cv.LINE_AA,
                )
            text_x += self.unitSize
            # self.draw_show()
            
        text_y = (self.height* self.unitSize)
        for y in range(self.height):
            cv.putText(self.img,str(text_y),
                (self.width * self.imgCell,y * self.imgCell),
                font,0.4,(255, 255, 255),2,cv.LINE_AA,
                )
            text_y -= self.unitSize 
            # self.draw_show()

    def draw_reset(self):
        self.img = copy.copy(self.table_org_img)

    def draw_show(self):
        cv.imshow("img", self.img)

    def draw_show_wait_key(self,showTime=100):
        cv.imshow("img", self.img)
        c = cv.waitKey(showTime) & 0xFF

